#include "ADUGameOverScene.h"
#include "ADUAppDelegate.h"
#include "ADUJavaCaller.h"

Scene* GameOverLayer::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = GameOverLayer::create();
    // add layer as a child to scene
    scene->addChild(layer,1);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool GameOverLayer::init()
{
    //////////////////////////////
    // 1. super init first
    if (!CCLayerColor::initWithColor(Color4B(0, 0, 0, 125)))
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    float centerX = origin.x + visibleSize.width/2;
    float centerY = origin.y + visibleSize.height/2;

    //this->addChild(CCLayerColor::create(ccc4(0, 0, 0, 0), visibleSize.width, visibleSize.height), Z_GAMEOVER_BG);

    // 2. Add background
    auto bg = Sprite::create("images/gameover_bg.png");
    bg->setPosition(centerX, centerY);
    this->addChild(bg, Z_GAMEOVER_ITEM);

    float bg_width = bg->getContentSize().width;
    float bg_height = bg->getContentSize().height;

    // 3. Add image of duck
    auto duck = Sprite::create("images/end_game.png");
    duck->setPosition(centerX, centerY);
    this->addChild(duck, Z_GAMEOVER_ITEM);

    float duck_height = duck->getContentSize().height;
    // 4. Add button
	mHighScore = Sprite::create(IMG_HIGHSCORE_PNG);
	mReplay = Sprite::create(IMG_REPLAY);
	mNextLevel = Sprite::create(IMG_NEXTLEVEL);
	mShareFacebook = Sprite::create(IMG_SHAREFB);

	float btn_width = mHighScore->getContentSize().width;
	float btn_height = mHighScore->getContentSize().height;
	float deltaWidth = (bg_width - 4*btn_width)/5;
	float deltaHeight = (bg_height - duck_height)/4;

	mHighScore->setPosition(centerX - 1.5*(deltaWidth + btn_width), centerY - duck_height/2 - deltaHeight);
	mReplay->setPosition(centerX - 0.5*(deltaWidth + btn_width), centerY - duck_height/2 - deltaHeight);
    mNextLevel->setPosition(centerX + 0.5*(deltaWidth + btn_width), centerY - duck_height/2 - deltaHeight);
    mShareFacebook->setPosition(centerX + 1.5*(deltaWidth + btn_width), centerY - duck_height/2 - deltaHeight);

	this->addChild(mHighScore, Z_GAMEOVER_ITEM);
	this->addChild(mReplay, Z_GAMEOVER_ITEM);
	this->addChild(mShareFacebook, Z_GAMEOVER_ITEM);
	this->addChild(mNextLevel, Z_GAMEOVER_ITEM);

	// 5. Add Title
	// Create score label
	// Add game title
    auto mLabelScore = Text::create("Game Over", FONT, FONT_SIZE);
    mLabelScore->setPosition(Vec2(centerX, centerY + duck_height/2 + deltaHeight));
    mLabelScore->setColor(Color3B(255,255,255));
    this->addChild(mLabelScore,1000);

    this->setTouchEnabled(true);

    return true;
}

void GameOverLayer::update(float dt) {

}

void GameOverLayer::setTouchEnabled(bool enabled)
{
    if (enabled)
    {
        mTouchListener = EventListenerTouchOneByOne::create();
        mTouchListener->retain();
        mTouchListener->onTouchBegan = CC_CALLBACK_2(GameOverLayer::onTouchBegan, this);
        mTouchListener->onTouchMoved = CC_CALLBACK_2(GameOverLayer::onTouchMoved, this);
        mTouchListener->onTouchEnded = CC_CALLBACK_2(GameOverLayer::onTouchEnded, this);
        mTouchListener->onTouchCancelled = CC_CALLBACK_2(GameOverLayer::onTouchCancelled, this);
        _eventDispatcher->addEventListenerWithSceneGraphPriority(mTouchListener, this);

    }
    else if (!enabled)
    {
        _eventDispatcher->removeEventListener(mTouchListener);
        mTouchListener->release();
        mTouchListener = nullptr;
    }

}


bool GameOverLayer::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {

    if(mHighScore->getBoundingBox().containsPoint(touch->getLocation())) {
        OnHighScoreClicked();
    } else if(mReplay->getBoundingBox().containsPoint(touch->getLocation())) {
        OnReplayClicked();
    } else if(mNextLevel->getBoundingBox().containsPoint(touch->getLocation())) { 
        OnNextLevelClicked();
    } else if(mShareFacebook->getBoundingBox().containsPoint(touch->getLocation())) {
        OnShareFacebookClicked();
    } 


}
void GameOverLayer::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event) {

}
void GameOverLayer::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event) {

}
void GameOverLayer::onTouchCancelled(cocos2d::Touch *touch, cocos2d::Event *event) {

}


void GameOverLayer::OnHighScoreClicked() {
    log("Hight Score clicked !!!");
}
void GameOverLayer::OnReplayClicked() {
    log("Replay clicked !!!");
    //GameManager::getInstance()->destroyAllBody();
    GameManager::getInstance()->RunScene(GAMEPLAY_SCENE);
    JavaCaller::showFullScreenAd();
}
void GameOverLayer::OnNextLevelClicked() {
    log("Next level clicked !!!");
    //GameManager::getInstance()->destroyAllBody();
    GameManager::getInstance()->RunScene(GAMEPLAY_SCENE);
}
void GameOverLayer::OnShareFacebookClicked() {
    log("Share facebook !!!");
    utils::captureScreen(CC_CALLBACK_2(GameOverLayer::OnAfterCaptured, this),"screenshot.png");
    
}

void GameOverLayer::OnAfterCaptured(bool scceed, const std::string& output){
    if (scceed){
        log("tuyendh - captured %s", output.c_str());
        JavaCaller::share();
    }else{
        log("tuyendh - capture failed");
    }
}

